package leetcode.code1760;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

//
public class Solution2 implements IDebug {

	public int minimumSize(int[] nums, int maxOperations) {
		int ans = 0;
		int max = 100000;
//		for (int num : nums) {
//			max = Math.max(max, num);
//		}
		int l = 1, r = max;
		while (l <= r) {
			int m = l + ((r - l) >> 1);
			int cut = 0;
			for (int num : nums) {
				if (num > m) {
					cut += num / m;
					if (num % m == 0) {
						cut -= 1;
					}
					if (cut > maxOperations) {
						break;
					}
				}
			}
			if (cut > maxOperations) {
				l = m + 1;
			} else {
				ans = m;
				r = m - 1;
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(7, this.minimumSize(HelpDebug.str2array3("[7,17]"), 2));
	}

	@Override
	public void debug2() {
		HelpDebug.compare(2, this.minimumSize(HelpDebug.str2array3("[2,6]"), 2));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub
	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}

}
